from dbs import Dao

class AdminDao(Dao):
    def __init__(self):
        super(AdminDao, self).__init__()
        self.table = 't_sys_user'

    def login(self, name, pwd):
        return self.query(self.table,
                          'uid as _id',
                          'username',
                          'create_time',
                          'role_id',
                          where='username=%s and password=%s',
                          args=(name, pwd), one=True)

    def get_role(self, role_id):
        role = self.query('t_sys_role', 'name', 'code', where='role_id=%s', args=(role_id, ), one=True)
        sql = """
        select m.title, m.url from t_role_menu trm 
        join t_sys_menu m on(m.menu_id=trm.menu_id)
        where trm.role_id=%s
        """
        menus = self.raw(sql, args=(role_id, ))
        role['menus'] = menus
        return role

    def get_permissions(self, role_id):
        # 通过存储过程，获取当前用户角色相关权限
        return self.call('proc_role_perms', args=(role_id, ))
